/**
 * 
 */
package bai8.com.client;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import bai8.com.command.Message;
import bai8.com.command.MessageFactory;

/**
 * @author nglehien Oct 21, 2012 - 10:29:53 PM
 */
public class ClientGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8965799181938840452L;
	private JPanel contentPane;
	private JButton btnUpper;
	private JTextArea textArea;
	private ClientConnection connection;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientGUI frame = new ClientGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public ClientGUI() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 449, 318);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		btnUpper = new JButton("Upper");
		btnUpper.addActionListener(onClickUpper);
		btnUpper.setBounds(10, 171, 89, 23);
		contentPane.add(btnUpper);

		textArea = new JTextArea();
		textArea.setBounds(10, 11, 414, 149);
		contentPane.add(textArea);

		btnLower = new JButton("Lower");
		btnLower.addActionListener(onClickLower);
		btnLower.setBounds(109, 171, 89, 23);
		contentPane.add(btnLower);

		btnInsert = new JButton("Insert");
		btnInsert.addActionListener(onClickInsert);
		btnInsert.setBounds(10, 239, 89, 23);
		contentPane.add(btnInsert);

		btnDelete = new JButton("delete");
		btnDelete.addActionListener(onClickDelete);
		btnDelete.setBounds(10, 205, 89, 23);
		contentPane.add(btnDelete);

		txtIndex = new JTextField();
		txtIndex.setBounds(152, 240, 99, 20);
		contentPane.add(txtIndex);
		txtIndex.setColumns(10);

		txtStr = new JTextField();
		txtStr.setBounds(271, 240, 152, 20);
		contentPane.add(txtStr);
		txtStr.setColumns(10);

		txtDelete = new JTextField();
		txtDelete.setBounds(112, 206, 312, 20);
		contentPane.add(txtDelete);
		txtDelete.setColumns(10);

		JLabel lblIndex = new JLabel("Index");
		lblIndex.setBounds(109, 243, 38, 14);
		contentPane.add(lblIndex);

		JButton btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					connection = new ClientConnection("127.0.0.1", 9999);
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnConnect.setBounds(208, 171, 89, 23);
		contentPane.add(btnConnect);
	}

	private ActionListener onClickInsert = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {

			int index = Integer.parseInt(txtIndex.getText());
			String data = txtStr.getText();
			Message message = MessageFactory.createInsertCommand(
					textArea.getText(), index, data);
			try {
				connection.sendToServer(message);
				message = connection.receiveFromServer();
				textArea.setText(message.getData());
			} catch (IOException | ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	private ActionListener onClickLower = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {

			Message message = MessageFactory.createLowerCommand(textArea
					.getText());
			try {
				connection.sendToServer(message);
				message = connection.receiveFromServer();
				textArea.setText(message.getData());
			} catch (IOException | ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	private ActionListener onClickUpper = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {

			Message message = MessageFactory.createUpperCommand(textArea
					.getText());
			try {
				connection.sendToServer(message);
				message = connection.receiveFromServer();
				textArea.setText(message.getData());
			} catch (IOException | ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	private ActionListener onClickDelete = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			String data = txtDelete.getText();
			Message message = MessageFactory.createDeleteCommand(
					textArea.getText(), data);
			try {
				connection.sendToServer(message);
				message = connection.receiveFromServer();
				textArea.setText(message.getData());
			} catch (IOException | ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	private JButton btnLower;
	private JButton btnInsert;
	private JButton btnDelete;
	private JTextField txtIndex;
	private JTextField txtStr;
	private JTextField txtDelete;
}
